<?php
/**
 *会议记录
 *
 * To change this template use File | Settings | File Templates.
 */
class MinutesModel extends Model
{
    static  $_instance   = null;
    private $_table     = 'crm_minutes';

    static function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 会议记录列表
     *
     * @param  string $sorts
     * @param  int  $page
     * @param  int  $pagesize
     * @return array
     */
    public function getList($sorts = 'id',$agency_id = 0, $page = 1, $pagesize = 10)
    {
        $contition = $agency_id == 1 ? '' : ' and find_in_set('.$agency_id.', dis_agency_id)';
        $sql = 'select *
                from '.$this->_table.'
                where stop=0 '.$contition.' order by '.$sorts.' desc ';
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;
        $list = $this->sql($sql.$limit)->s();
        foreach ($list as $key=>$item) {
            $tmp = array();
            $row = StaffModel::getInstance()->getInfo($item['participant'], false);
            foreach ($row as $il) {
                $tmp[] = $il['name'];
            }
            $item['participant_name'] = implode(',', $tmp);
            $list[$key] = $item;
        }
        return $list;
    }

    public function getCount($agency_id = 0)
    {
        $contition = $agency_id == 1 ? '' : ' and find_in_set('.$agency_id.', dis_agency_id)';
        $list = $this->sql('select count(*) as c from '.$this->_table.' where stop=0 '.$contition)->s(1);
        return $list['c'];
    }
    /**
     * 添加会议记录
     *
     * @param  array    $data
     * @return bool
     */
    public function add($data)
    {
        $sql = insertSql($this->_table, $data);
        return $this->sql($sql)->i();
    }

    /**
     * 修改会议记录
     *
     * @param  array    $data
     * @param  int      $id
     * @return bool
     */
    public function edit($data, $id)
    {
        $sql = updateSql($this->_table, $data, 'id='.$id);
        @file_put_contents('sql.txt', $sql."\r\n");
        return $this->sql($sql)->u();
    }

    /**
     * 删除会议记录
     *
     * @param  int  $id
     * @return bool
     */
    public function del($id, $in=true)
    {
        if ($in) {
            $sql = "update  ".$this->_table." set stop=1 where id=".$id;
        } else {
            $sql = "update  ".$this->_table." set stop=1 where id in (".$id.")";
        }
        return $this->sql($sql)->d();
    }

    /**
     * 获取会议记录详情
     *
     * @param  int  $id
     * @return array
     */
    public function getInfo($id)
    {
        $rows = $this->sql('select * from '.$this->_table.' where id= '.$id)->s(1);
        $tmp = array();
        $row = StaffModel::getInstance()->getInfo($rows['participant'], false);
        foreach ($row as $il) {
            $tmp[] = $il['name'];
        }
        $rows['participant_name'] = implode(',', $tmp);

        return $rows;
    }
}